package com.project.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }
    //创建队列
    @Bean
    public Queue queueOne(){
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "directOrderDeadExchangeOrder");
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key","rabbit.order.dead");
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",8000);
        return QueueBuilder.durable("queueOrder").withArguments(args).build();
    }
    @Bean
    public Queue orderDeadOne(){
        return new Queue("orderDeadOne");
    }

    //创建交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("directExchangeOrder");
    }
    @Bean
    public DirectExchange directOrderDeadExchangeOrder(){
        return new DirectExchange("directOrderDeadExchangeOrder");
    }
    @Bean
    public Binding directExchangeToQueueOne(Queue queueOne,DirectExchange directExchange){
        return   BindingBuilder.bind(queueOne).to(directExchange).with("rabbit.order");
    }
    @Bean
        public Binding directDeadExchangeToDeadQueueOne(Queue orderDeadOne,DirectExchange directOrderDeadExchangeOrder){
        return   BindingBuilder.bind(orderDeadOne).to(directOrderDeadExchangeOrder).with("rabbit.order.dead");
    }
}
